import 'package:enterprise_v6_app/app/core/core.dart';

///自定义进度条
class CustomProgressComp extends StatelessWidget {
  const CustomProgressComp({
    required this.width,
    required this.height,
    required this.bgColor,
    this.linearGradient,
    super.key,
    this.value = 1,
    this.title,
    this.num,
    this.isShowIcon,
    this.isShowTitle,
    this.topTitle,
  });

  // 宽度-必填
  final double width;

  // 高度-必填
  final double height;

  // 背景色
  final Color bgColor;

  // 当前比例(当前值/总数值)-必填
  final double value;

  // 显示左侧标题
  final String? title;

  // 显示右侧值
  final String? num;

  // 渐变背景
  final LinearGradient? linearGradient;

  // 是否显示上方标题信息
  final bool? isShowTitle;

  /// 是否显示进度条右侧图标
  final bool? isShowIcon;

  // 自定义上方标题信息
  final Widget? topTitle;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Visibility(
          visible: isShowTitle ?? true,
          child: Row(
            children: [
              AppGaps.hGap5,
              Text(
                title.toString(),
                style: TextStyle(color: const Color(0xff333333).withOpacity(0.5), fontSize: 12.w, fontWeight: FontWeight.w400),
              ),
              const Spacer(),
              Text(
                num.toString(),
                style: TextStyle(color: const Color(0xff417EFB), fontSize: 12.w, fontWeight: FontWeight.w400),
              ),
              AppGaps.hGap5,
            ],
          ),
        ),
        topTitle ?? Container(),
        Container(
            width: width,
            height: 20.w,
            alignment: Alignment.center,
            child: Stack(
              children: <Widget>[
                Container(
                  width: width,
                  height: height,
                  margin: EdgeInsets.only(top: 5.w),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(height)),
                    color: bgColor,
                  ),
                ),
                Stack(
                  children: [
                    Container(
                      height: height,
                      margin: EdgeInsets.only(top: 5.w, bottom: 6.w, right: 4.w),
                      decoration: BoxDecoration(
                        gradient: linearGradient ??
                            const LinearGradient(begin: Alignment.centerRight, end: Alignment.centerLeft, colors: [
                              Color(0xff469CF1),
                              Color(0xff9DCFFF),
                            ]),
                        borderRadius: BorderRadius.all(Radius.circular(height)),
                      ),
                      width: value * width,
                    ),
                    Visibility(
                      visible: isShowIcon ?? true,
                      child: Positioned(
                        right: 0,
                        child: Image.asset(
                          '${AppImages.image}icon_progress_comp.png',
                          width: 16.w,
                          height: 16.w,
                        ),
                      ),
                    )
                  ],
                )
              ],
            )),
        AppGaps.vGap12,
      ],
    );
  }
}

/// 带有间隔、不同颜色的进度条
class CustomDividedProgress extends StatelessWidget {
  const CustomDividedProgress({
    /// 每一部分占多少
    required this.vals,

    /// 每一部分的颜色
    required this.colors,

    /// 总共多少
    required this.total,
    super.key,

    /// 间隔大小
    this.gapSize,

    /// 高度
    this.height,
  }) : assert(vals.length == colors.length, 'vals.length must equal colors.length');

  final List<double> vals;
  final List<Color> colors;
  final int total;
  final double? gapSize;
  final double? height;

  @override
  Widget build(BuildContext context) {
    return Flex(
      direction: Axis.horizontal,
      children: _buildItems(),
    );
  }

  List<Widget> _buildItems() {
    final children = <Widget>[];
    for (int i = 0; i < vals.length + 1; i++) {
      children.add(_buildItem(i));
      if (i != vals.length) {
        children.add(SizedBox(width: gapSize ?? 2.w));
      }
    }
    return children;
  }

  Widget _buildItem(int idx) {
    if (idx == vals.length) {
      return Expanded(
        flex: (total - vals.reduce((a, b) => a + b).toInt()) * 10,
        child: Container(),
      );
    }
    final val = vals[idx];
    final color = colors[idx];
    return Expanded(
      flex: (val * 10).toInt(),
      child: Container(
        height: height ?? 4.w,
        decoration: BoxDecoration(
            gradient: LinearGradient(
          colors: [
            color.withOpacity(0.3),
            color,
          ],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        )),
      ),
    );
  }
}
